//
//  CXHGroMoreNativeHelper.m
//  CXHAdSDK-GroMore
//
//  Created by 陈坤 on 2024/10/15.
//

#import "CXHGroMoreNativeHelper.h"
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdViewDelegate.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdData.h>

@interface CXHGroMoreNativeHelper ()

@property (nonatomic, strong) UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView;
@property (nonatomic, strong) UILabel *adTitleLabel;
@property (nonatomic, strong) UIImageView *adImageView;

@end

@implementation CXHGroMoreNativeHelper

- (instancetype)initWithAdData:(UIView<CXHAdapterNativeAdViewDelegate> *)nativeAdView {
    if (self = [super init]) {
        _nativeAdView = nativeAdView;
        
        [self setupView];
    }
    return self;
}

- (void)setupView{
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.nativeAdView.frame = CGRectMake(0, 0, 200, 200);
        strongSelf.adTitleLabel = [[UILabel alloc] init];
        strongSelf.adImageView = [[UIImageView alloc] init];
    });
}

#pragma mark - BUMMediatedNativeAdViewCreator

- (UILabel *)titleLabel {
    return self.adTitleLabel;
}

- (UIView *)adLogoView{
    return [self.nativeAdView cxh_logoView];
}

- (UIImageView *)imageView{
    [self.nativeAdView cxh_loadImage:self.adImageView placeholder:nil];
    return self.adImageView;
}

#pragma mark - BUMMediatedNativeAdData

- (NSString *)AdTitle {
    return [self.nativeAdView cxh_nativeAdData].title;
}

- (NSString *)AdDescription {
    return [self.nativeAdView cxh_nativeAdData].desc;
}

- (NSArray<BUMImage *> *)imageList {
    BUMImage *img = [[BUMImage alloc] init];
    img.imageURL = [NSURL URLWithString:[self.nativeAdView cxh_nativeAdData].imageUrl];
    return @[img];
}

- (BUMImage *)adLogo {
    BUMImage *img = [[BUMImage alloc] init];
    img.imageURL = [NSURL URLWithString:[self.nativeAdView cxh_nativeAdData].iconImageUrl];
    return img;
}

- (BUMImage *)sdkLogo {
    return self.adLogo;
}

- (UIView *)mediaView {
    return [self.nativeAdView cxh_mediaViewForWidth:self.nativeAdView.frame.size.width];
}

- (BUMMediatedNativeAdMode)imageMode {
    return BUMMediatedNativeAdModeLargeImage;
}

@end
